package com.gitee.jktantan.datasource.monitor.analyze.entity;

import lombok.Getter;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * 计时实体
 */
@Getter
public class TimerEntity {
    //计数
    private String count;
    //平均时间
    private String meanRate;
    //最近1分钟平均时间
    private String m1Rate;
    //最近5分钟平均时间
    private String m5Rate;
    //最近15分钏平均时间
    private String m15Rate;

    private String min;
    private String max;
    private String mean;
    private String stdDev;
    private String p50;
    private String p75;
    private String p95;
    private String p98;
    private String p99;
    private String p999;

    protected double convertRate(double rate) {
        return rate * TimeUnit.SECONDS.toSeconds(1);
    }

    protected double convertDuration(double duration) {
        return duration / TimeUnit.MILLISECONDS.toNanos(1);
    }

    public void setCount(long count) {
        this.count = String.format(Locale.getDefault(), "%d", count);
    }

    public void setMeanRate(double meanRate) {
        this.meanRate = String.format(Locale.getDefault(), "%2.2f", convertRate(meanRate));
    }

    public void setM1Rate(double m1Rate) {
        this.m1Rate = String.format(Locale.getDefault(), "%2.2f", convertRate(m1Rate));
    }

    public void setM5Rate(double m5Rate) {
        this.m5Rate = String.format(Locale.getDefault(), "%2.2f", convertRate(m5Rate));
    }

    public void setM15Rate(double m15Rate) {
        this.m15Rate = String.format(Locale.getDefault(), "%2.2f", convertRate(m15Rate));
    }

    public void setMin(double min) {
        this.min = String.format(Locale.getDefault(), "%2.2f", convertDuration(min));
    }

    public void setMax(double max) {
        this.max = String.format(Locale.getDefault(), "%2.2f", convertDuration(max));
    }

    public void setMean(double mean) {
        this.mean = String.format(Locale.getDefault(), "%2.2f", convertDuration(mean));
    }

    public void setStdDev(double stdDev) {
        this.stdDev = String.format(Locale.getDefault(), "%2.2f", convertDuration(stdDev));
    }

    public void setP50(double p50) {
        this.p50 = String.format(Locale.getDefault(), "%2.2f", convertDuration(p50));
    }

    public void setP75(double p75) {
        this.p75 = String.format(Locale.getDefault(), "%2.2f", convertDuration(p75));
    }

    public void setP95(double p95) {
        this.p95 = String.format(Locale.getDefault(), "%2.2f", convertDuration(p95));
    }

    public void setP98(double p98) {
        this.p98 = String.format(Locale.getDefault(), "%2.2f", convertDuration(p98));
    }

    public void setP99(double p99) {
        this.p99 = String.format(Locale.getDefault(), "%2.2f", convertDuration(p99));
    }

    public void setP999(double p999) {
        this.p999 = String.format(Locale.getDefault(), "%2.2f", convertDuration(p999));
    }
}
